<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Request;

use app\admin\model\ComValidate;
use app\admin\model\ComModel;

class Link extends Common{

	public function _initialize(){
		if(request()->isGet()){

			$action= strtolower ( Request::instance()->action() );

			switch ($action) {
		
				case 'linklist':
					$this->assign('title',"链接列表");
					$controller="link";
					break;
				case 'linkinfo':
					$this->assign('title',"链接信息");
					$controller="link";
					break;
			}

			$this->assign('controller',$controller);
		}
	}

	#link列表#
	public function linklist(){

		if(request()->isGet()){

			return $this->fetch();
		}

	}
	#link列表接口#
	public function jsonlinklist(){
		
			$page=input('page');
			$limit=input('limit');

			$start=input('start');
			$end=input('end');

			if($start){
				$map['addtime']=array('egt',$start);
			}
			if($end){
				$map['addtime']=array('elt',$end);
			}
			if($start&&$end){
				$map['addtime']=array('between',$start.",".$end);
			}

			$list=Db::name('link_list')->page($page.','.$limit)->where($map)->order('px desc')->select();
			
			foreach ($list as $key => $v) {

				if($v['status']=='1'){
					$list[$key]['status']="<span class='layui-badge-rim layui-bg-green'>正常</span>";
				}else{
					$list[$key]['status']="<span class='layui-badge-rim layui-bg-gray'>下架</span>"; 
				}
			}
			$count=Db::name('link_list')->count('linkid');

			$arr=array(
				'code'=>"0",
				'count'=>$count,
				'data'=>$list
			);
			return json($arr);
			//json($list);
	}

	#编辑link信息#
	public function linkinfo(){
		if(request()->isGet()){

			$linkid=input('linkid');
		
			if($linkid){

				$this->assign('info',ComModel::data_find('link_list',['linkid'=>$linkid]));
			}

			$this->assign('videocate',$videocate);
			return $this->fetch();

		}else{

			$this->log("编辑link信息");

			$data=input('post.');
	
			$data['status']=empty($data['status'])?"0":$data['status'];

			if($data['linkid']){

				ComModel::data_up('link_list',$data,url('linklist'));

			}else{

				ComModel::data_add('link_list',$data,url('linklist'));

			}
		}
	}

	#删除link#
	public function linkdel(){

		$this->log("删除link");
		
		$linkid=input('linkid');

		ComValidate::data_must($linkid);

		echo ComModel::data_del('link_list',['linkid'=>$linkid]);
	}


}